package com.health.warehouse.controller;


import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.health.common.common.BaseResponse;
import com.health.common.common.ErrorCode;
import com.health.common.common.ResultUtils;
import com.health.common.exception.BusinessException;
import com.health.api.model.transporter.AddWareHouseItemRequest;
import com.health.warehouse.model.dto.AddWareHouseRequest;
import com.health.warehouse.model.dto.MedicineTransferRequest;
import com.health.warehouse.model.dto.linkItemToWareHouseRequest;
import com.health.warehouse.model.vo.WarehouseItemVO;
import com.health.warehouse.service.WarehouseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static com.health.common.constant.UserRoleConstant.HOSPITAL;
import static com.health.common.constant.UserRoleConstant.TRANSPORTER;

/**
 * @author zhexueqi
 * @ClassName WareHouseController
 * @since 2024/9/1    15:48
 */
@RestController
@Tag(name = "仓库管理", description = "仓库管理相关接口")
public class WareHouseController {

    @Resource
    private WarehouseService wareHouseService;

    /**
     * 新增仓库
     * @param addWareHouseRequest
     * @return
     */
    @PostMapping("/addWareHouse")
    @Operation(summary = "新增仓库", description = "新增仓库")
    @SaCheckRole(HOSPITAL)
    public boolean addWareHouse(@RequestBody @Valid AddWareHouseRequest  addWareHouseRequest){
        if (addWareHouseRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_NULL, "参数不能为空");
        }
        return wareHouseService.addWareHouse(addWareHouseRequest);
    }

    /**
     * 新增药物在仓库中(但未放在具体的仓库中)
     * @param addWareHouseItemRequest
     * @return boolean
     */
    @PostMapping("/addWareHouseItem")
    @Operation(summary = "新增药物在仓库中但未放在具体的仓库中(运输商角色)", description = "新增药物在仓库中但未放在具体的仓库中(运输商角色)")
    @SaCheckRole(TRANSPORTER)
    public boolean addWareHouseItem(@RequestBody @Valid AddWareHouseItemRequest addWareHouseItemRequest){
        if (addWareHouseItemRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_NULL, "参数不能为空");
        }
        return wareHouseService.addWareHouseItem(addWareHouseItemRequest);
    }

    /**
     * 医院角色将药物关联到仓库中
     * @param linkItemToWareHouseRequest
     * @return boolean
     */
    @PostMapping("/linkItemToWareHouse")
    @Operation(summary = "医院角色将药物关联到仓库中", description = "医院角色将药物关联到仓库中")
    @SaCheckRole(HOSPITAL)
    public boolean linkItemToWareHouse(@RequestBody @Valid linkItemToWareHouseRequest linkItemToWareHouseRequest) {
        if (linkItemToWareHouseRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_NULL, "参数不能为空");
        }
        return wareHouseService.linkItemToWareHouse(linkItemToWareHouseRequest);
    }

    /**
     * 医院角色查看有哪些还未分配的药物
     * @param request 请求
     */
    @PostMapping("/getNotAllocatedItems")
    @Operation(summary = "医院角色查看有哪些还未分配的药物", description = "医院角色查看有哪些还未分配的药物")
    @SaCheckRole(HOSPITAL)
    public Page<WarehouseItemVO> getNotAllocatedItems(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,@RequestParam(value = "pageSize",defaultValue = "10") int pageSize, HttpServletRequest request) {
        return wareHouseService.getNotAllocatedItems(request,pageNum,pageSize);
    }

    /**
     * 跨医院药物调取申请
     *
     * @param
     * @param medicineTransferRequest
     */
    @PostMapping("/medicineTransferRequest")
    @Operation(summary = "跨医院药物调取申请", description = "跨医院药物调取申请")
    @SaCheckRole(HOSPITAL)
    public BaseResponse<Boolean> medicineTransferRequest(@RequestBody @Valid MedicineTransferRequest medicineTransferRequest, HttpServletRequest request) {
        if (medicineTransferRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_NULL, "参数不能为空");
        }
        boolean result = wareHouseService.medicineTransferRequest(medicineTransferRequest,request);
        if (!result){
            throw new BusinessException(ErrorCode.ERROR_REQUEST,"申请失败");
        }
        return ResultUtils.success(true);
    }

    /**
     * 医院用户同意跨医院药物调取申请
     */
    @PostMapping("/agreeMedicineTransferRequest")
    @Operation(summary = "医院用户同意跨医院药物调取申请", description = "医院用户同意跨医院药物调取申请")
    @SaCheckRole(HOSPITAL)
    public BaseResponse<Boolean> agreeMedicineTransferRequest(@RequestParam("medicineTransferId") Long medicineTransferId) {
        if (medicineTransferId == null){
            throw new BusinessException(ErrorCode.PARAMS_NULL, "参数不能为空");
        }
        boolean result = wareHouseService.agreeMedicineTransferRequest(medicineTransferId);
        if (!result){
            throw new BusinessException(ErrorCode.ERROR_REQUEST,"同意失败");
        }
        return ResultUtils.success(true);
    }




}
